package com.labichaoka.chaoka.ui.credit;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseActivity;
import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.LimitStatusResponse;
import com.labichaoka.chaoka.ui.MainActivity;
import com.labichaoka.chaoka.utils.ToastUtils;

import butterknife.OnClick;

/*
* 非种子用户额度激活页面
*
* */

public class CalculateActivity extends BaseActivity implements CalculateView{

    private CalculatePresenter presenter;
    private Dialog dialog;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_calculate;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        dialog = new Dialog(mContext, R.style.Translucent_NoTitle);
        presenter=new CalculatePresenterImpl(new CalculateInteractorImpl(),this);
        presenter.calculate();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @OnClick(R.id.back)
    public void click(View v){
        MyApplication.finishAllActivityExceptMain(MainActivity.class);
    }

    @Override
    public void showProgress() {
        showLoadingProgress();
    }

    @Override
    public void hideProgress() {
        hideLoadingProgress();
    }

    @Override
    public void showMessage(String str) {
        ToastUtils.show(str);
    }

    private void showNoticeDialog(int type,String msg) {
        final LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.dialog_calculate_result, null);
        dialog.setContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);

        LinearLayout ok=v.findViewById(R.id.ok_layout);
        TextView str=v.findViewById(R.id.str);
        str.setText(msg);
        TextView ok_txt=v.findViewById(R.id.ok_txt);
        if (type==1){
            ok_txt.setText("我知道了");
        }else{
            ok_txt.setText("重新验证");
        }
        ok.setOnClickListener((view)->{
            dialog.dismiss();
            if (type==1){
                MyApplication.finishAllActivityExceptMain(MainActivity.class);
            }else{
                finish();
            }
        });

        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialog.onWindowAttributesChanged(wl);
        dialog.setCanceledOnTouchOutside(false);

        dialog.show();

    }

    @Override
    public void setData(LimitStatusResponse response) {
        String status=response.getData().getQuotaStatus();
        if (TextUtils.isEmpty(status)){
            ToastUtils.show("获取数据异常!");
            return;
        }
        switch (status){
            case "1":
                //成功
                MyApplication.finishAllActivityExceptMain(MainActivity.class);
                break;
            case "1001":
                // 计算中
                break;
//            case "-1":
//                showNoticeDialog(1,"很遗憾，您所提交的资料未能通过额度计算，我们将无法继续为您提供相应的服务。");
//                // 失败
//                break;
//            case "-2":
//                // 失败
//                showNoticeDialog(2,"您的资料在验证时出现问题，我们建议您重新进行验证并计算额度。");
//                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }
}
